import request from '@/utils/request'

// 查询企业资讯列表
export function listNews(query) {
  return request({
    url: '/system/news/list',
    method: 'get',
    params: query
  })
}

// 查询企业资讯详细
export function getNews(id) {
  return request({
    url: '/system/news/' + id,
    method: 'get'
  })
}

// 上传news文件
export function uploadNewsFile(data) {
  return request({
    url: '/common/upload',
    method: 'post',
    headers: { 'Content-Type': 'multipart/form-data' },
    data: data
  })
}

// 新增企业资讯
export function addNews(data) {
  // 将数据转换为URLSearchParams格式，适配@RequestParam接收
  const formData = new URLSearchParams()
  Object.keys(data).forEach(key => {
    if (data[key] !== null && data[key] !== undefined) {
      formData.append(key, data[key])
    }
  })
  
  return request({
    url: '/system/news',
    method: 'post',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    data: formData
  })
}

// 修改企业资讯
export function updateNews(data) {
  // 将数据转换为URLSearchParams格式，适配@RequestParam接收
  const formData = new URLSearchParams()
  Object.keys(data).forEach(key => {
    if (data[key] !== null && data[key] !== undefined) {
      formData.append(key, data[key])
    }
  })
  
  return request({
    url: '/system/news',
    method: 'put',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    data: formData
  })
}

// 删除企业资讯
export function delNews(id) {
  return request({
    url: '/system/news/' + id,
    method: 'delete'
  })
}
